<?php
namespace common\models;


class TradeOrder extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'trade_order';
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['mch_id', 'member_id', 'invite_id', 'btype', 'goods_id', 'num', 'create_time', 'update_time'], 'integer'],
            [['member_id','order_type'], 'required'],
            [['amount'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'order_id' => 'Order ID',
            'mch_id' => 'Mch ID',
            'member_id' => 'Member ID',
            'invite_id' => '推荐人',
            'btype' => '操作的账本',
            'order_type' => '订单类型 与goods对应',
            'goods_id' => '产品id',
            'amount' => '订单金额',
            'num' => '数量',
            'period'=>'签约周期',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }    

	/*
		* 数据列表
		* whereArr 条件
		* params 基本参数 包含 field order page limit
		* extends  扩展信息 一些相关的信息
		* 
	*/	
	public static function dataList($whereArr, $params, $extends = array())
	{
		$model  = self::find();
		$where  = isset($whereArr['where']) ? $whereArr['where'] : [];
		$whereAnd = isset($whereArr['and'])?$whereArr['and']:[];
		$models = self::queryFormart($model,$where,$params,$whereAnd);
		$model  = $models['model'];
		
		$data  = $model->asarray()->all();

		if(!$data) return array();

		//扩展信息
		if(!$extends) return $data;

		foreach($extends as $extend)
		{
		}

		return $data;	
	}

	public static function getOne($where, $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->one();
	}


	public static function getAll($where = [], $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->all();
	}

	public   function addData($arr,$member){
	    if (empty($arr))return false;
        $data = array();
        $data['mch_id']     = 1;
        $data['member_id']  = $member['member_id']??165;
        $data['invite_id']  = $member['invite_id']??0;
        $data['btype']      = $arr['btype']??1;
        $data['order_type'] = $arr['type'];
        $data['goods_id']   = $arr['goods_id'];
        $data['amount']     = $arr['amount'];
        $data['period']     = $arr['period']??0;
        $data['create_time'] = time();
        $data['update_time'] = time();
        $model = new TradeOrder();
        if($model->load($data,'') && $model->save()){

            return \Yii::$app->db->getLastInsertID();
        }
        return false;
    }


}